package com.example.a2032.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.a2032.R;

public class BatteryView extends View {
    Paint mPaint;
    int lowBatteryColor = Color.parseColor("#ff2424");
    int normalBatteryColor = Color.DKGRAY;
    int chargeBatteryColor = Color.parseColor("#00db0a");

    Bitmap bgBitmap;
    Bitmap flashBitmap;
    float scaleHeightValue;
    float scaleWidthValue;
    //屏幕宽高直接写死
    float screenWidth = 320f;
    float screenHeight = 320f;
    Paint textPaint;

    public BatteryView(Context context) {
        super(context);
    }

    public BatteryView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setColor(normalBatteryColor);
        mPaint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(12);
        textPaint.setAntiAlias(true);
        bgBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.battery_stroke);
        flashBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.battery_flash);
        scaleWidthValue = screenWidth / 320;
        scaleHeightValue = screenHeight / 360;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension((int) (bgBitmap.getWidth() * scaleWidthValue), (int) (bgBitmap.getHeight() * scaleHeightValue));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBatteryLevel(canvas);
        drawBackground(canvas);


    }

    private void drawBackground(Canvas canvas) {
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidthValue, scaleHeightValue);
        canvas.drawBitmap(bgBitmap, matrix, null);
        float centerX = bgBitmap.getWidth() * scaleWidthValue / 2f;
        float centerY = bgBitmap.getHeight() * scaleHeightValue / 2f;
        if (isCharging) drawFlash(canvas, centerX, centerY);
        drawBatteryText(canvas, centerX, centerY);
    }

    void drawFlash(Canvas canvas, float centerX, float centerY) {
        canvas.save();
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidthValue, scaleHeightValue);
        canvas.translate(centerX - flashBitmap.getWidth() / 2, 0);
        canvas.drawBitmap(flashBitmap, matrix, null);
        canvas.restore();
    }

    int batteryLevel = 15;

    private void drawBatteryLevel(Canvas canvas) {
        float progress = batteryLevel / 100f;
        RectF rectF = new RectF();
        rectF.left = 3;
        rectF.right = rectF.left + 27 * progress;
        rectF.top = 3f;
        rectF.bottom = rectF.top + 12f;
        if (isCharging) {
            mPaint.setColor(chargeBatteryColor);
        } else {
            mPaint.setColor(batteryLevel < 20 ? lowBatteryColor : normalBatteryColor);
        }
        canvas.drawRect(rectF, mPaint);

    }

    boolean isCharging;

    public void setBatteryLevel(int batteryLevel, boolean isCharging) {
        this.batteryLevel = batteryLevel;
        this.isCharging = isCharging;
        postInvalidate();
    }

    private static final String TAG = "BatteryView";

    void drawBatteryText(Canvas canvas, float x, float centerY) {
        Log.i(TAG, "drawBatteryText: " + batteryLevel);
        String batteryText = batteryLevel + "";
        Rect rect = new Rect();
        textPaint.getTextBounds(batteryText, 0, batteryText.length(), rect);
        Paint.FontMetricsInt fm = textPaint.getFontMetricsInt();
        float startY = centerY - fm.descent + (fm.bottom - fm.top) / 2;
        canvas.drawText(batteryText, x - rect.width() / 2 - 3, startY, textPaint);
    }
}
